package com.bootdo.cms.service.impl;

import com.bootdo.cms.dao.ChannelDao;
import com.bootdo.cms.domain.ChannelDO;
import com.bootdo.cms.service.ChannelService;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.BuildTree;
import com.bootdo.system.domain.DeptDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true,rollbackFor = Exception.class)
public class ChannelServiceImpl implements ChannelService {
	@Autowired
	ChannelDao channelMapper;
	/**
	 * @param
	 * @return 树形栏目
	 */
	@Cacheable
	@Override
	public Tree<ChannelDO> getCmsChannelTree(Long id) {
		List<Tree<ChannelDO>> trees = new ArrayList<>();
		List<ChannelDO> channelDOs = channelMapper.listChannel();
		for (ChannelDO channelDO : channelDOs) {
			Tree<ChannelDO> tree = new Tree<ChannelDO>();
			tree.setId(channelDO.getChannelId().toString());
			tree.setParentId(channelDO.getParentId().toString());
			tree.setText(channelDO.getName());
			Map<String, Object> attributes = new HashMap<>(16);
			attributes.put("url", channelDO.getUrl());
			attributes.put("icon", channelDO.getIcon());
			tree.setAttributes(attributes);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<ChannelDO> t = BuildTree.build(trees);
		return t;
	}

	@Override
	public List<ChannelDO> list(Map<String, Object> params) {
		List<ChannelDO> channels = channelMapper.list(params);
		return channels;
	}

	@Transactional(readOnly = false,rollbackFor = Exception.class)
	@Override
	public int remove(Long id) {
		int result = channelMapper.remove(id);
		return result;
	}
	@Transactional(readOnly = false,rollbackFor = Exception.class)
	@Override
	public int save(ChannelDO channel) {
		int r = channelMapper.save(channel);
		return r;
	}

	@Transactional(readOnly = false,rollbackFor = Exception.class)
	@Override
	public int update(ChannelDO channel) {
		int r = channelMapper.update(channel);
		return r;
	}

	@Override
	public ChannelDO get(Long id) {
		ChannelDO channelDO = channelMapper.get(id);
		return channelDO;
	}

	@Override
	public List<Long> listChildrenIds(Long parentId) {
		List<ChannelDO> ChannelDOS = list(null);
		return treeMenuList(ChannelDOS, parentId);
	}

	@Override
	public Tree<ChannelDO> getTree() {
		List<Tree<ChannelDO>> trees = new ArrayList<Tree<ChannelDO>>();
		List<ChannelDO> channelDOs = channelMapper.list(new HashMap<>(16));
		for (ChannelDO channelDO : channelDOs) {
			Tree<ChannelDO> tree = new Tree<ChannelDO>();
			tree.setId(channelDO.getChannelId().toString());
			tree.setParentId(channelDO.getParentId().toString());
			tree.setText(channelDO.getName());
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<ChannelDO> t = BuildTree.build(trees);
		return t;
	}

/*	@Override
	public Tree<ChannelDO> getTree(Long id) {
		// 根据roleId查询权限
		List<ChannelDO> channels = channelMapper.list(new HashMap<String, Object>(16));
		List<Long> menuIds = roleMenuMapper.listMenuIdByRoleId(id);
		List<Long> temp = menuIds;
		for (ChannelDO menu : channels) {
			if (temp.contains(menu.getParentId())) {
				menuIds.remove(menu.getParentId());
			}
		}
		List<Tree<ChannelDO>> trees = new ArrayList<Tree<ChannelDO>>();
		List<ChannelDO> channelDOs = channelMapper.list(new HashMap<String, Object>(16));
		for (ChannelDO channelDO : channelDOs) {
			Tree<ChannelDO> tree = new Tree<ChannelDO>();
			tree.setId(channelDO.getMenuId().toString());
			tree.setParentId(channelDO.getParentId().toString());
			tree.setText(channelDO.getName());
			Map<String, Object> state = new HashMap<>(16);
			Long menuId = channelDO.getMenuId();
			if (menuIds.contains(menuId)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<ChannelDO> t = BuildTree.build(trees);
		return t;
	}*/

/*	@Override
	public Set<String> listPerms(Long userId) {
		List<String> perms = channelMapper.listUserPerms(userId);
		Set<String> permsSet = new HashSet<>();
		for (String perm : perms) {
			if (StringUtils.isNotBlank(perm)) {
				permsSet.addAll(Arrays.asList(perm.trim().split(",")));
			}
		}
		return permsSet;
	}*/

	@Override
	public List<Tree<ChannelDO>> listChannelTree() {
		List<Tree<ChannelDO>> trees = new ArrayList<Tree<ChannelDO>>();
		List<ChannelDO> channelDOs = channelMapper.listChannel();
		for (ChannelDO channelDO : channelDOs) {
			Tree<ChannelDO> tree = new Tree<ChannelDO>();
			tree.setId(channelDO.getChannelId().toString());
			tree.setParentId(channelDO.getParentId().toString());
			tree.setText(channelDO.getName());
			Map<String, Object> attributes = new HashMap<>(16);
			attributes.put("url", channelDO.getUrl());
			attributes.put("icon", channelDO.getIcon());
			tree.setAttributes(attributes);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		List<Tree<ChannelDO>> list = BuildTree.buildList(trees, "0");
		return list;
	}

	List<Long> treeMenuList(List<ChannelDO> menuList, long pid) {
		List<Long> childIds = new ArrayList<>();
		for (ChannelDO mu : menuList) {
			//遍历出父id等于参数的id，add进子节点集合
			if (mu.getParentId() == pid) {
				//递归遍历下一级
				treeMenuList(menuList, mu.getChannelId());
				childIds.add(mu.getChannelId());
			}
		}
		return childIds;
	}

}
